package com.ylq.controller;

import com.ylq.entity.Users;
import com.ylq.secret.MD5Utils;
import com.ylq.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Author qiang
 * @Description TODO
 * @Date 2020-04-30
 **/
@RestController
@Api(description = "Token令牌获取")
@RequestMapping("/api")
public class A_TokenAccess {
    @Autowired
    private UsersService usersService;

    @ApiOperation("获取数据库密钥")
    @PostMapping("/getToken")
    public Object getToken(HttpSession session, @ApiParam("登陆用户名")@RequestParam("un") String un, @ApiParam("登陆密码")@RequestParam("pwd") String pwd){
        String token = MD5Utils.getMD5Str(un);
        Users login = usersService.login(un, pwd);
        session.setAttribute("token",token) ;
        if (login!=null){
            return "数据库密钥为："+token;
        }else {
            return "用户名或密码不存在";
        }
    }
}
